package com.ny.dao.impl;

import com.ny.dao.ProductDao;
import com.ny.entity.Product;
import com.ny.until.Sqlconnect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    Connection cont = Sqlconnect.getCont();
    public ProductDaoImpl() throws SQLException, ClassNotFoundException {
    }

    @Override
    public List<Product> findAllProduct() throws SQLException {
        String sql = "select * from products";
        PreparedStatement  pst = cont.prepareStatement(sql);
        ResultSet  rs = pst.executeQuery();
        List<Product> productList = new ArrayList<>();
        while  (rs.next()){
            Product product = new Product();
            product.setProduct_id(rs.getInt("product_id"));
            product.setName(rs.getString("name"));
            product.setPrice(rs.getDouble("price"));
            product.setStock(rs.getString("stock"));
            product.setCategory(rs.getString("category"));
            product.setDescription(rs.getString("description"));
            productList.add(product);
        }

        return productList;
    }

    @Override
    public int addProduct(Product product) throws SQLException {
        String sql = "insert into products(name,price,stock,category,description) values(?,?,?,?,?)";
        PreparedStatement  pst = cont.prepareStatement(sql);
        pst.setString(1,product.getName());
        pst.setDouble(2,product.getPrice());
        pst.setInt(3,product.getStock());
        pst.setString(4,product.getCategory());
        pst.setString(5,product.getDescription());
        return pst.executeUpdate();
    }

    @Override
    public Product updateProduct(Product product) throws SQLException {
        String sql = "update products set name = ?,price = ?,stock = ?,category = ?,description = ? where product_id = ?";
        PreparedStatement  pst = cont.prepareStatement(sql);
        pst.setString(1,product.getName());
        pst.setDouble(2,product.getPrice());
        pst.setInt(3,product.getStock());
        pst.setString(4,product.getCategory());
        pst.setString(5,product.getDescription());
        return pst.executeUpdate() > 0 ? product : null;
    }

    @Override
    public Product deleteProduct(String product_name) throws SQLException {
        String sql = "delete from products where name = ?";
        PreparedStatement  pst = cont.prepareStatement(sql);
        pst.setString(1,product_name);
        int i = pst.executeUpdate();
        if (i > 0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
        return null;
    }

    @Override
    public boolean reduceStock(int productId, int quantity) throws SQLException {
        String sql = "UPDATE products SET stock = stock - ? WHERE product_id = ? ";
        PreparedStatement pst = cont.prepareStatement(sql);
        pst.setInt(1, quantity);
        pst.setInt(2, productId);// 确保库存足够
        int rowsAffected = pst.executeUpdate();
        return rowsAffected > 0;
    }
}
